package com.hirsi.module.role.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hirsi.core.vo.param.PageParam;
import com.hirsi.core.vo.result.PageResult;
import com.hirsi.module.role.entity.Role;
import com.hirsi.module.role.mapper.RoleMapper;
import com.hirsi.module.role.service.RoleService;
import com.hirsi.module.role.vo.EditRoleVo;
import com.hirsi.module.role.vo.param.ChangeRoleStatusParam;
import com.hirsi.module.role.vo.param.QueryRoleParam;
import com.hirsi.module.role.vo.param.SaveRolePermissionParam;
import com.hirsi.module.role.vo.result.RoleResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@ApiSupport(order = 4)
@RestController
@RequestMapping("v1/role")
public class RoleController {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleService roleService;

    @ApiOperation("分页查询角色列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("page")
    public PageResult<RoleResult> page(PageParam page, QueryRoleParam param) {
        return PageResult.parse(roleMapper.selectRoleResult(page.pager(), param));
    }

    @ApiOperation("编辑回显角色")
    @ApiOperationSupport(order = 2)
    @GetMapping("edit")
    public EditRoleVo edit(@ApiParam(value = "角色id", required = true) @RequestParam Integer id) {
        return roleService.edit(id);
    }

    @ApiOperation("保存角色")
    @ApiOperationSupport(order = 3)
    @PostMapping("save")
    public void save(@RequestBody EditRoleVo vo) {
        roleService.save(vo);
    }

    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 4)
    @PostMapping("delete")
    public void delete(@RequestBody List<Integer> ids) {
        roleService.delete(ids);
    }

    @ApiOperation("修改角色状态")
    @ApiOperationSupport(order = 5)
    @PostMapping("change_status")
    public void changeStatus(@RequestBody ChangeRoleStatusParam param) {
        roleService.lambdaUpdate().eq(Role::getId, param.getId())
                .set(Role::getStatus, param.getStatus()).update();
    }

    @ApiOperation("获取角色具有的菜单权限")
    @ApiOperationSupport(order = 6)
    @GetMapping("permission")
    public List<Integer> permission(@ApiParam(value = "角色id", required = true) @RequestParam Integer id) {
        return roleService.permission(id);
    }

    @ApiOperation("保存角色权限")
    @ApiOperationSupport(order = 7)
    @PostMapping("save_permission")
    public void savePermission(@RequestBody SaveRolePermissionParam param) {
        roleService.savePermission(param);
    }
}
